<?php
/**
 * Prado Portal.
 *
 * @author Steen Rabol <steen.rabol@gmail.com>
 * @link http://www.pradoportal.dk/
 * @copyright Copyright &copy; 2006,2007,2008 Steen Rabol
 * @license http://www.pradoportal.dk
 * @version $Id: RolePermissions.php 323 2008-12-01 18:32:53Z steen.rabol@gmail.com $
 *
 */

Prado::using('Application.Common.Data.PortalPageRecord');

class RolePermissions extends PortalAdminPage
{
	public function getPages()
	{
		return PortalPageRecord::finder()->findAll('order by path');
	}

	public function getGroupData()
	{
		return $this->DataAccess->createCommand("select tblpermissions.*,tblroles.name as groupname from tblpermissions,tblroles where selector='role' && tblroles.id = tblpermissions.value order by page")->query();
	}

	public function getPermissionGroups()
	{
		return $this->DataAccess->createCommand("select * from tblroles order by name")->query();
	}

	public function onLoad($param)
	{
		parent::onLoad($param);

		$this->setAdminPageTitle('Role permissions');

		if(!$this->IsPostBack)
		{
			$this->DataList->DataSource = $this->GroupData;
			$this->DataList->dataBind();

			$this->RoleId->DataSource = $this->PermissionGroups;
			$this->RoleId->dataBind();

			$this->dlPage->DataSource = $this->getPages();
			$this->dlPage->dataBind();
		}
	}

	public function editItem($sender, $param)
	{
		$this->DataList->SelectedItemIndex	= -1;
		$this->DataList->EditItemIndex		= $param->Item->ItemIndex;
		$this->DataList->DataSource			= $this->GroupData;
		$this->DataList->dataBind();
	}

	public function cancelItem($sender, $param)
	{
		$this->DataList->SelectedItemIndex	= -1;
		$this->DataList->EditItemIndex		= -1;
		$this->DataList->DataSource			= $this->GroupData;
		$this->DataList->dataBind();
	}

	public function updateItem($sender, $param)
	{
		$item	= $param->Item;
		$id		= $this->DataList->DataKeys[$item->ItemIndex];
		$page	= $item->dlEditPage->SelectedValue;
		$roleid = $item->dlEditRole->SelectedValue;
		$view	= $item->CanView->SelectedValue;
		$edit	= $item->CanEdit->SelectedValue;

		if($page == "All")
		{
			$sql = "update tblpermissions set page=null, view='$view', edit='$edit', value='$roleid' where id='$id'";
		}
		else
		{
			$sql = "update tblpermissions set page='$page', view='$view', edit='$edit', value='$roleid' where id='$id'";
		}

		$this->DataAccess->createCommand($sql)->execute();
		$this->DataList->EditItemIndex = -1;
		$this->DataList->SelectedItemIndex = $item->ItemIndex;
		$this->DataList->DataSource = $this->GroupData;
		$this->DataList->dataBind();
	}

	public function deleteItem($sender, $param)
	{
		$item = $param->Item;
		$id = $this->DataList->DataKeys[$item->ItemIndex];
		$this->DataAccess->createCommand("delete from tblpermissions  where id='$id'")->execute();
		$this->DataList->EditItemIndex = -1;
		$this->DataList->SelectedItemIndex = -1;
		$this->DataList->DataSource = $this->GroupData;
		$this->DataList->dataBind();
	}

	public function AddRolePermision($sender, $param)
	{
		$page = $this->dlPage->SelectedValue;
		$roleid = $this->RoleId->SelectedValue;
		$canview = $this->CanView->SelectedValue;
		$canedit = $this->CanEdit->SelectedValue;

		if($page == "All")
		{
			$sql = "insert tblpermissions(selector,value,view,edit) values('role','$roleid','$canview','$canedit')";
		}
		else
		{
			$sql = "insert tblpermissions(page,selector,value,view,edit) values('$page','role','$roleid','$canview','$canedit')";
		}

		$this->DataAccess->createCommand($sql)->execute();
		$this->DataList->DataSource = $this->GroupData;
		$this->DataList->dataBind();
	}

	public function itemCreated($sender, $param)
	{
		$item = $param->Item;

		if ($item->ItemType === 'EditItem')
		{
			$item->dlEditPage->DataSource = $this->getPages();
			$item->dlEditRole->DataSource = $this->getPermissionGroups();
		}
	}

	public function getAvailableSystemPages()
	{
		$basePath = $this->Application->BasePath.'/Pages/System';
		$layouts = array();

		if(!is_dir($basePath))
		{
			$this->reportError(1, "Could not find layout directory $basePath");
		}

		$folder = @opendir($basePath);

		while($file = @readdir($folder))
		{
			if($file!=='.' && $file!=='..' && $file!=='.svn' && !is_dir($basePath.'/'.$file))
			{
				$path_parts = pathinfo($file);
				if($path_parts['extension'] == "php")
				{
					$layouts[] = array('name' => 'System.'.basename($path_parts['basename'], '.php'));
				}
			}
		}

		closedir($folder);
		return $layouts;
	}
}
?>